package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.db.domain.MarketBrandExample;
import com.cskaoyan.market.db.mapper.MarketBrandMapper;
import com.cskaoyan.market.service.AdminBrandService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

public class AdminBrandServiceImpl implements AdminBrandService {
    @Override
    public List<MarketBrand> list(String page, String limit, String sort, String order, String id, String name) {
        SqlSession session = MybatisUtil.getSession();
        MarketBrandMapper brandMapper = session.getMapper(MarketBrandMapper.class);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.parseInt(id));
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
//        page = "10";
//        limit = "1";
        marketBrandExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketBrand> marketBrands = brandMapper.selectByExample(marketBrandExample);
        session.commit();
        session.close();
        return marketBrands;

    }

    @Override
    public MarketBrand create(String name, String desc, String picUrl, String floorPrice) {
        SqlSession sqlSession = MybatisUtil.getSession();
        MarketBrandMapper brandMapper = sqlSession.getMapper(MarketBrandMapper.class);
        //MarketBrandExample marketBrandExample = new MarketBrandExample();
        //MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        MarketBrand marketBrand = new MarketBrand();

        BigDecimal floorPriceDecimal = new BigDecimal(floorPrice);
        marketBrand.setName(name);
        marketBrand.setDesc(desc);
        marketBrand.setPicUrl(picUrl);
        marketBrand.setFloorPrice(floorPriceDecimal);
        if (StringUtils.isEmpty(picUrl)) {
            marketBrand.setPicUrl("");
        }
        marketBrand.setAddTime(LocalDateTime.now());
        marketBrand.setUpdateTime(LocalDateTime.now());
        brandMapper.insert(marketBrand);
        sqlSession.commit();
        sqlSession.close();
        return marketBrand;
    }

    @Override
    public int delete(String id) {
        SqlSession session = MybatisUtil.getSession();
        MarketBrandMapper brandMapper = session.getMapper(MarketBrandMapper.class);
        brandMapper.deleteByPrimaryKey(Integer.parseInt(id));
        session.commit();
        session.close();
        return 0;

    }

    @Override
    public int update(String name, String desc, String picUrl, String floorPrice, LocalDateTime addTime, LocalDateTime updateTime, Boolean deleted) {
        SqlSession session = MybatisUtil.getSession();
        MarketBrandMapper brandMapper = session.getMapper(MarketBrandMapper.class);
        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setPicUrl(picUrl);
        BigDecimal floorPriceDecimal = new BigDecimal(floorPrice);
        marketBrand.setFloorPrice(floorPriceDecimal);
        marketBrand.setDesc(desc);
        marketBrand.setAddTime(addTime);
        marketBrand.setUpdateTime(updateTime);
        marketBrand.setDeleted(deleted);
        int i = brandMapper.updateByPrimaryKey(marketBrand);
        session.commit();
        session.close();
        return i;
    }

}
